package com.zwface.demo.function.web;

import android.webkit.WebSettings;
import android.webkit.WebView;

public class WebviewUtils
{
    public static void loadWebview(WebView webView,String html)
    {
        //支持javascript
        webView.getSettings().setJavaScriptEnabled(true);
        //扩大比例的缩放
        webView.getSettings().setUseWideViewPort(false);
        //自适应屏幕
        webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        webView.getSettings().setLoadWithOverviewMode(true);

        int width = com.blankj.utilcode.util.ScreenUtils.getScreenWidth();

        StringBuilder sb = new StringBuilder();
        sb.append(getHtmlData(html));
        html = sb.toString();

        webView.loadDataWithBaseURL(null, html, "text/html", "utf-8", null);


    }

    /**
     * 加载html标签
     *
     * @param bodyHTML
     * @return
     */
    private static String getHtmlData(String bodyHTML) {
        String head = "<head>" +
                "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\"> " +
                //"<style>img{max-width: 100%; width:auto; height:auto!important;} html{width:100%}</style>" +
                "</head>";

        head = "<head>"
                + "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\"> "
                + "<style>img{max-width: 100%; width:100%; height:auto;}</style>"
                + "<style>table{max-width: 100%; width:100%; height:auto;}</style>"
                + "</head>";

        return "<html>" + head + "<body>" + bodyHTML + "</body></html>";
    }

}
